#define _CRT_SECURE_NO_WARNINGS 1

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* swapPairs(struct ListNode* head) {
    if (head == NULL || head->next == NULL)
        return head;
    struct ListNode* n1 = head, * n2 = head->next, * n3 = head->next->next, * pret = head->next;
    while (n2 && n2->next)
    {
        n2->next = n1;
        if (n3->next == NULL)
        {
            n1->next = n3;
            return pret;
        }
        n1->next = n3->next;
        n1 = n3;
        n2 = n1->next;
        if (n2 == NULL)
            break;
        n3 = n2->next;
    }
    n3 = NULL;
    if (n2 != NULL)
        n2->next = n1;
    n1->next = n3;
    return pret;
}